package top.lingchen.blogapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.lingchen.blogapi.bean.Comment;
import top.lingchen.blogapi.bean.SysUser;
import top.lingchen.blogapi.mapper.CommentMapper;
import top.lingchen.blogapi.service.CommentsService;
import top.lingchen.blogapi.service.SysUserService;
import top.lingchen.blogapi.utils.UserThreadLocal;
import top.lingchen.blogapi.vo.CommentVo;
import top.lingchen.blogapi.vo.Result;
import top.lingchen.blogapi.vo.params.CommentParam;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author 凌宸
 * @create 2021-12-17 下午 2:50
 * @Description
 * @Version 1.0
 */
@Service
public class CommentsServiceImpl implements CommentsService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private SysUserService sysUserService;

    @Override
    public Result findCommentsByArticleId(Long articleId) {
        /**
         * 根据文章id 查询评论列表  从 comment 表中查询
         * 根据作者的 id 查询作者的信息
         * 判断level 是否等于 1，决定是否要进行查询它的子评论
         * 如果有子评论，根据评论 id 查询(parentId)
         */
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Comment::getArticleId, articleId);
        wrapper.eq(Comment::getLevel, 1);
        List<Comment> comments = commentMapper.selectList(wrapper);
        return Result.success(copyList(comments));
    }

    @Override
    public Result comment(CommentParam commentParam) {
        SysUser sysUser = UserThreadLocal.get();
        Comment comment = new Comment();
        comment.setArticleId(commentParam.getArticleId());
        comment.setContent(commentParam.getContent());
        comment.setAuthorId(sysUser.getId());
        comment.setCreateDate(System.currentTimeMillis());
        Long parent = commentParam.getParent();
        if(parent == null || parent == 0){
            comment.setLevel(1);
        }else {
            comment.setLevel(2);
        }
        comment.setParentId(parent == null ? 0 : parent);
        Long toUserId = commentParam.getToUserId();
        comment.setToUid(toUserId == null ? 0 : toUserId);
        commentMapper.insert(comment);
        return Result.success(null);
    }

    private List<CommentVo> copyList(List<Comment> comments) {
        List<CommentVo> commentVoList = new ArrayList<>();
        for (Comment comment : comments) {
            commentVoList.add(copy(comment));
        }
        return commentVoList;
    }

    private CommentVo copy(Comment comment) {
        CommentVo commentVo = new CommentVo();
        BeanUtils.copyProperties(comment, commentVo);
        commentVo.setId(String.valueOf(comment.getId()));
        // 设置作者信息
        commentVo.setAuthor(sysUserService.findUserVoById(comment.getAuthorId()));
        // 子评论
        Integer level = comment.getLevel();
        if(1 == level){
            List<CommentVo> commentVoList = findCommentsByParentId(comment.getId());
            commentVo.setChildrens(commentVoList);
        }
        // to user 给谁评论
        if(level > 1){
            commentVo.setToUser(sysUserService.findUserVoById(comment.getToUid()));
        }
        return commentVo;
    }

    private List<CommentVo> findCommentsByParentId(Long parentId) {
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Comment::getParentId, parentId);
        wrapper.eq(Comment::getLevel, 2);
        return copyList(commentMapper.selectList(wrapper));
    }
}
